package magnus.minimap;

import magnus.minimap.MinimapService.ServiceHandler;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.maps.MapActivity;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;

public class Minimap extends MapActivity {
	SharedPreferences preferences;
	private MapView mMapView;
    private MyLocationOverlay mMyLocationOverlay;
    private TextView mTextView;
    private MinimapService mMinimapService;
    private MinimapHandler minimaphandler;
    private ServiceHandler servicehandler;
    private LocationManager mLocationManager;

    
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        // Init Preferences Pane
        preferences = PreferenceManager.getDefaultSharedPreferences(this);
        preferences.registerOnSharedPreferenceChangeListener(onPreferenceChange);
        
        // Init Text View
        mTextView = (TextView) findViewById(R.id.TextView01);
        
        // Init Map View
        mMapView = (MapView) findViewById(R.id.map1);
        
        // Start Modified Handler Class (IPC)
        minimaphandler = new MinimapHandler() {};
        
        // Start Service
		mMinimapService = new MinimapService(minimaphandler,preferences);
		
        // Start Monitoring Location 
		
		mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0.0f,onLocationChange);
        
		// Get Service Handler (IPC)
		servicehandler = mMinimapService.getServiceHandler();
		
        // Enable Location Dot
        mMyLocationOverlay = new MyLocationOverlay(this, mMapView);
        mMyLocationOverlay.enableMyLocation();
        mMyLocationOverlay.runOnFirstFix(new Runnable() { public void run() {
            mMapView.getController().animateTo(mMyLocationOverlay.getMyLocation());
            Location gp = mMyLocationOverlay.getLastFix();
        	servicehandler.sendMessage(Message.obtain(servicehandler, servicehandler.NEW_POSITION_DATA_AVAILABLE,gp));
        }});
        mMapView.getOverlays().add(mMyLocationOverlay);
        mMapView.getController().setZoom(30);
        
    }

    LocationListener onLocationChange = new LocationListener() {
        public void onLocationChanged(Location mLocation) {servicehandler.sendMessage(Message.obtain(servicehandler, servicehandler.NEW_POSITION_DATA_AVAILABLE,mLocation));}
        public void onProviderDisabled(String provider) {}
        public void onProviderEnabled(String provider) {}
        public void onStatusChanged(String provider, int status, Bundle extras) {}
    };
    
    public void plot_locations(Friend []friends) {
    	int i = -1;
    	while (++i < friends.length) {
    		//friends[i].location;
    	}
    }
    
    public class MinimapHandler extends Handler {
    	public final int DEBUG_TOAST_MESSAGE = 10;
    	public final int DEBUG_TEXTVIEW_SET = 11;
    	public final int PLOT_LOCATIONS_REQUEST = 12;
    	public void handleMessage(Message message) {
			switch (message.what) {
				case DEBUG_TOAST_MESSAGE: {Toast.makeText(getApplicationContext(), (String) message.obj, Toast.LENGTH_SHORT).show();break;} // TODO missing break statement
				case DEBUG_TEXTVIEW_SET: {mTextView.setText((String) message.obj);break;}//String a = ":) " + mLocation.getLatitude() + mLocation.getLongitude();
				case PLOT_LOCATIONS_REQUEST: {plot_locations((Friend [])message.obj);}
			}//Switch
		}
    }
    
	@Override
	protected boolean isRouteDisplayed() {return false;}

	// Below Preferences
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	MenuInflater inflater = getMenuInflater();
    	inflater.inflate(R.menu.menu, menu);
    	return true;
    }
    @Override
    public boolean onOptionsItemSelected (MenuItem item) {
    	switch (item.getItemId()) {
    	case R.id.preferences:
    		Intent i = new Intent(Minimap.this, Preferences.class);
    		startActivity(i);
    		break;
    	}
    	return true;
    }
    OnSharedPreferenceChangeListener onPreferenceChange = new OnSharedPreferenceChangeListener() {
		public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
			servicehandler.sendMessage(Message.obtain(servicehandler, servicehandler.PREFERENCES_CHANGED));
		}
    };
}